<?php
	check_login();
	$a = getAction();
	
	$menuTypeList = array();
	$menuTypeList[] = array("key" => "click", "value" => "点击事件");
	$menuTypeList[] = array("key" => "view", "value" => "跳转URL");
	$menuTypeList[] = array("key" => "scancode_push", "value" => "扫码推事件");
	$menuTypeList[] = array("key" => "scancode_waitmsg", "value" => "扫码推事件且弹出“消息接收中”提示框");
	$menuTypeList[] = array("key" => "pic_sysphoto", "value" => "弹出系统拍照发图");
	$menuTypeList[] = array("key" => "pic_photo_or_album", "value" => "弹出拍照或者相册发图");
	$menuTypeList[] = array("key" => "pic_weixin", "value" => "弹出微信相册发图器");
	$menuTypeList[] = array("key" => "location_select", "value" => "弹出地理位置选择器");
	
	$smarty->assign("menuTypeList", $menuTypeList);

	switch ($a) {
		case "index":
			$rs=$db->getRow("select * from ".table('wxmenu'));
			$smarty->assign("rs",$rs);
			$smarty->display("wx_menu.html");
			break;
		case "add_db":
	        $appid=$_POST['appid'];
	        $appsecret=$_POST['appsecret'];
			$menu1name=$_POST['menu1name'];
	        $menu1key=$_POST['menu1key'];
	        $menu1type=$_POST['menu1type'];
	        $menu1submenu1name=$_POST['menu1submenu1name'];
	        $menu1submenu1key=$_POST['menu1submenu1key'];
	        $menu1submenu2name=$_POST['menu1submenu2name'];
	        $menu1submenu2key=$_POST['menu1submenu2key'];
	        $menu1submenu3name=$_POST['menu1submenu3name'];
	        $menu1submenu3key=$_POST['menu1submenu3key'];
	        $menu1submenu4name=$_POST['menu1submenu4name'];
	        $menu1submenu4key=$_POST['menu1submenu4key'];
	        $menu1submenu5name=$_POST['menu1submenu5name'];
	        $menu1submenu5key=$_POST['menu1submenu5key'];
	        $menu1submenu1type=$_POST['menu1submenu1type'];
	        $menu1submenu2type=$_POST['menu1submenu2type'];
	        $menu1submenu3type=$_POST['menu1submenu3type'];
	        $menu1submenu4type=$_POST['menu1submenu4type'];
	        $menu1submenu5type=$_POST['menu1submenu5type'];
	        
	        $menu2name=$_POST['menu2name'];
	        $menu2key=$_POST['menu2key'];
	        $menu2type=$_POST['menu2type'];
	        $menu2submenu1name=$_POST['menu2submenu1name'];
	        $menu2submenu1key=$_POST['menu2submenu1key'];
	        $menu2submenu2name=$_POST['menu2submenu2name'];
	        $menu2submenu2key=$_POST['menu2submenu2key'];
	        $menu2submenu3name=$_POST['menu2submenu3name'];
	        $menu2submenu3key=$_POST['menu2submenu3key'];
	        $menu2submenu4name=$_POST['menu2submenu4name'];
	        $menu2submenu4key=$_POST['menu2submenu4key'];
	        $menu2submenu5name=$_POST['menu2submenu5name'];
	        $menu2submenu5key=$_POST['menu2submenu5key'];
	        $menu2submenu1type=$_POST['menu2submenu1type'];
	        $menu2submenu2type=$_POST['menu2submenu2type'];
	        $menu2submenu3type=$_POST['menu2submenu3type'];
	        $menu2submenu4type=$_POST['menu2submenu4type'];
	        $menu2submenu5type=$_POST['menu2submenu5type'];
	        
	        $menu3name=$_POST['menu3name'];
	        $menu3key=$_POST['menu3key'];
	        $menu3type=$_POST['menu3type'];
	        $menu3submenu1name=$_POST['menu3submenu1name'];
	        $menu3submenu1key=$_POST['menu3submenu1key'];
	        $menu3submenu2name=$_POST['menu3submenu2name'];
	        $menu3submenu2key=$_POST['menu3submenu2key'];
	        $menu3submenu3name=$_POST['menu3submenu3name'];
	        $menu3submenu3key=$_POST['menu3submenu3key'];
	        $menu3submenu4name=$_POST['menu3submenu4name'];
	        $menu3submenu4key=$_POST['menu3submenu4key'];
	        $menu3submenu5name=$_POST['menu3submenu5name'];
	        $menu3submenu5key=$_POST['menu3submenu5key'];
	        $menu3submenu1type=$_POST['menu3submenu1type'];
	        $menu3submenu2type=$_POST['menu3submenu2type'];
	        $menu3submenu3type=$_POST['menu3submenu3type'];
	        $menu3submenu4type=$_POST['menu3submenu4type'];
	        $menu3submenu5type=$_POST['menu3submenu5type'];
        
			$ct = $db->getOne("select count(*) from ".table('wxmenu'));	
			
			$setting = $db->getRow("select * from " . table("setting"));
			if($setting) {
				$setting["appid"] = $appid;
				$setting["appsecret"] = $appsecret;
				
				$db->update("setting", $setting, "");
			} else {
				$setting["appid"] = $appid;
				$setting["appsecret"] = $appsecret;
				
				$db->insert("setting", $setting);
			}
			
			$sql = table('wxmenu')." set appid='$appid',appsecret='$appsecret',menu1name='$menu1name',menu1key='" . trim($menu1key) . "',menu1submenu1name='$menu1submenu1name',menu1submenu1key='" . trim($menu1submenu1key) . "',menu1submenu2name='$menu1submenu2name',menu1submenu2key='" . trim($menu1submenu2key) . "',menu1submenu3name='$menu1submenu3name',menu1submenu3key='" . trim($menu1submenu3key) . "',menu1submenu4name='$menu1submenu4name',menu1submenu4key='" . trim($menu1submenu4key) . "',menu1submenu5name='$menu1submenu5name',menu1submenu5key='" . trim($menu1submenu5key) . "',
		        menu2name='$menu2name',menu2key='" . trim($menu2key) . "',menu2submenu1name='$menu2submenu1name',menu2submenu1key='" . trim($menu2submenu1key) . "',menu2submenu2name='$menu2submenu2name',menu2submenu2key='" . trim($menu2submenu2key) . "',menu2submenu3name='$menu2submenu3name',menu2submenu3key='" . trim($menu2submenu3key) . "',menu2submenu4name='$menu2submenu4name',menu2submenu4key='" . trim($menu2submenu4key) . "',menu2submenu5name='$menu2submenu5name',menu2submenu5key='" . trim($menu2submenu5key) . "',
		        menu3name='$menu3name',menu3key='" . trim($menu3key) . "',menu3submenu1name='$menu3submenu1name',menu3submenu1key='" . trim($menu3submenu1key) . "',menu3submenu2name='$menu3submenu2name',menu3submenu2key='" . trim($menu3submenu2key) . "',menu3submenu3name='$menu3submenu3name',menu3submenu3key='" . trim($menu3submenu3key) . "',menu3submenu4name='$menu3submenu4name',menu3submenu4key='" . trim($menu3submenu4key) . "',menu3submenu5name='$menu3submenu5name',menu3submenu5key='" . trim($menu3submenu5key) . "',
		        menu1type='$menu1type', menu1submenu1type='$menu1submenu1type',menu1submenu2type='$menu1submenu2type',menu1submenu3type='$menu1submenu3type',menu1submenu4type='$menu1submenu4type',menu1submenu5type='$menu1submenu5type',
		        menu2type='$menu2type', menu2submenu1type='$menu2submenu1type',menu2submenu2type='$menu2submenu2type',menu2submenu3type='$menu2submenu3type',menu2submenu4type='$menu2submenu4type',menu2submenu5type='$menu2submenu5type',
		        menu3type='$menu1type', menu3submenu1type='$menu3submenu1type',menu3submenu2type='$menu3submenu2type',menu3submenu3type='$menu3submenu3type',menu3submenu4type='$menu3submenu4type',menu3submenu5type='$menu3submenu5type',
		        update_time= '" . getNowTime() . "'";
			if($ct==0) {
				$sql = "insert into $sql , insert_time = '" . getNowTime() . "'"; 
			} else {
				$sql = "update $sql";
			}

			$db->query($sql);
        
	        $wxmenupostarray = array();
			
			if($menu1name){
        		if($menu1key){
		            $menu1array = getMenuArray($menu1name, $menu1key, $menu1type);
		            $wxmenupostarray[] = $menu1array;
		        }else{
        		    $temp1array = array();
            		if($menu1submenu1name){
		                $menu1submenu1namearray = getMenuArray($menu1submenu1name, $menu1submenu1key, $menu1submenu1type, 0, 0);
		                $temp1array[] = $menu1submenu1namearray;
		            }
		            if($menu1submenu2name){
		                $menu1submenu2namearray = getMenuArray($menu1submenu2name, $menu1submenu2key, $menu1submenu2type, 0, 1);
		                $temp1array[] = $menu1submenu2namearray;
		            }
		            if($menu1submenu3name){
		                $menu1submenu3namearray = getMenuArray($menu1submenu3name, $menu1submenu3key, $menu1submenu3type, 0, 2);
		                $temp1array[] = $menu1submenu3namearray;
		            }
		            if($menu1submenu4name){
		                $menu1submenu4namearray = getMenuArray($menu1submenu4name, $menu1submenu4key, $menu1submenu4type, 0, 3);
		                $temp1array[] = $menu1submenu4namearray;
		            }
		            if($menu1submenu5name){
		                $menu1submenu5namearray = getMenuArray($menu1submenu5name, $menu1submenu5key, $menu1submenu5type, 0, 4);
		                $temp1array[] = $menu1submenu5namearray;
		            }
		            $menu1array = array("name"=>$menu1name,"sub_button"=>$temp1array);
		            $wxmenupostarray[] = $menu1array;
        		}
        	}
        
        
	        if($menu2name){
		        if($menu2key){
		            $menu2array = getMenuArray($menu2name, $menu2key, $menu2type, 1, 0);
		            $wxmenupostarray[] = $menu2array;
		        }else{
		            
		            $temp2array = array();
		            if($menu2submenu1name){
		                $menu2submenu1namearray = getMenuArray($menu2submenu1name, $menu2submenu1key, $menu2submenu1type, 1, 0);
		                $temp2array[] = $menu2submenu1namearray;
		            }
		            if($menu2submenu2name){
		                $menu2submenu2namearray = getMenuArray($menu2submenu2name, $menu2submenu2key, $menu2submenu2type, 1, 1);
		                $temp2array[] = $menu2submenu2namearray;
		            }
		            if($menu2submenu3name){
		                $menu2submenu3namearray = getMenuArray($menu2submenu3name, $menu2submenu3key, $menu2submenu3type, 1, 2);
		                $temp2array[] = $menu2submenu3namearray;
		            }
		            if($menu2submenu4name){
		                 $menu2submenu4namearray = getMenuArray($menu2submenu4name, $menu2submenu4key, $menu2submenu4type, 1, 3);
		                $temp2array[] = $menu2submenu4namearray;
		            }
		            if($menu2submenu5name){
		                $menu2submenu5namearray = getMenuArray($menu2submenu5name, $menu2submenu5key, $menu2submenu5type, 1, 4);
		                $temp2array[] = $menu2submenu5namearray;
		            }
		           $menu2array = array("name"=>$menu2name,"sub_button"=>$temp2array);
		           $wxmenupostarray[] = $menu2array;
		        }
	        }
        
	        if($menu3name){
		        if($menu3key){
		            $menu3array = getMenuArray($menu3name, $menu3key, $menu3type, 2, 0);
		            $wxmenupostarray[] = $menu3array;
		        }else{
		
		            $temp3array = array();
		            if($menu3submenu1name){
		                $menu3submenu1namearray = getMenuArray($menu3submenu1name, $menu3submenu1key, $menu3submenu1type, 2, 0);
		                $temp3array[] = $menu3submenu1namearray;
		            }
		            if($menu3submenu2name){
		                $menu3submenu2namearray = getMenuArray($menu3submenu2name, $menu3submenu2key, $menu3submenu2type, 2, 1);
		                $temp3array[] = $menu3submenu2namearray;
		            }
		            if($menu3submenu3name){
		                $menu3submenu3namearray = getMenuArray($menu3submenu3name, $menu3submenu3key, $menu3submenu3type, 2, 2);
		                $temp3array[] = $menu3submenu3namearray;
		            }
		            if($menu3submenu4name){
		                $menu3submenu4namearray = getMenuArray($menu3submenu4name, $menu3submenu4key, $menu3submenu4type, 2, 3);
		                $temp3array[] = $menu3submenu4namearray;
		            }
		            if($menu3submenu5name){
		                $menu3submenu5namearray = getMenuArray($menu3submenu5name, $menu3submenu5key, $menu3submenu5type, 2, 4);
		                $temp3array[] = $menu3submenu5namearray;
		            }
		            $menu3array = array("name"=>$menu3name,"sub_button"=>$temp3array);
		            $wxmenupostarray[] = $menu3array;
		        }
	        }
        
	        $wxarray =  array("button"=>$wxmenupostarray);
    	    $data = JSON($wxarray); 
			$requesturl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
			$json_ret = json_decode(file_get_contents($requesturl),true);
			$accesstoken = $json_ret['access_token'];
			$wxmenuurl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accesstoken;

			$wxmenuresjson = json_decode(menuPost($wxmenuurl, $data),true);
			if($wxmenuresjson['errcode'] == 0){
				$smarty->assign("return","微信自定义菜单配置成功！24小时内更新！取消关注该微信公众帐号，然后重新关注，立即生效！");          
				$smarty->display("wx_menu_return.html");
			}else{
				$smarty->assign("return","微信自定义菜单配置失败！请检查appid和appsecret是否填写正确！");         
				$smarty->display("wx_menu_return.html");
			}
			break;
	}

	function getMenuArray($name, $value, $type, $index1, $index2) {
		if($type == "view") {
			return array( "type"=>"view","name"=> $name,"url"=>$value);
		} else {
			if($type == "click") {
				return array( "type"=>"click","name"=> $name,"key"=>$value);
			}
			if(!$index1) $index1 = "0";
			if(!$index2) $index2 = "0";
			
			$key = "rselfmenu_" . $index1 . "_" . $index2;
			return array( "type"=>"$type","name"=> $name,"key"=>$key);
		}
	}

	function menuPost($url, $jsonData){
	    $ch = curl_init($url) ;
	    curl_setopt($ch, CURLOPT_POST, 1);
	    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	    curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonData);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
	    $result = curl_exec($ch) ;
	    curl_close($ch) ;
	    return $result;
	}
	function arrayRecursive(&$array, $function, $apply_to_keys_also = false) {
	    static $recursive_counter = 0;
	    if (++$recursive_counter > 1000) {
	        die('possible deep recursion attack');
	    }
	    foreach ($array as $key => $value) {
	        if (is_array($value)) {
	            arrayRecursive($array[$key], $function, $apply_to_keys_also);
	        } else {
	            $array[$key] = $function($value);
	        }
	
	        if ($apply_to_keys_also && is_string($key)) {
	            $new_key = $function($key);
	            if ($new_key != $key) {
	                $array[$new_key] = $array[$key];
	                unset($array[$key]);
	            }
	        }
	    }
	    $recursive_counter--;
	}
	function JSON($array) {
	    arrayRecursive($array, 'urlencode', true);
	    $json = json_encode($array);
	    return urldecode($json);
	}
?>